package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.AppVersion;
import com.mls.fivedistrict.server.AppVersionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by lixixi on 2018/4/2.
 */
@RestController
@RequestMapping("/api/manager")
public class AppVersionController extends BaseController {
    @Resource(name = "appVersionService")
    AppVersionService appVersionService;

    @GetMapping("/v1/version")
    public ServiceResponse getVersion() {
        AppVersion newestVersion = appVersionService.getNewestVersion();
        if (newestVersion != null) {
            return ServiceResponse.createSuccessResponse(newestVersion);
        } else {
            return ServiceResponse.createErrorResponse("没有最新版本");
        }
    }

    @GetMapping("/v1/app_versions")
    public ServiceResponse getVersions(HttpServletRequest httpServletRequest) {
        PageInfo<AppVersion> appVersionList = appVersionService.getAllList(getPageFilter());
        return ServiceResponse.createSuccessResponse(appVersionList);
    }

    @PostMapping("/v1/app_version")
    public ServiceResponse addVersion(@RequestBody AppVersion appVersion) {
//        Optional.ofNullable(appVersion.getAppVersion()).orElseThrow(() -> new CustomerException("版本号未添加"));
//        Optional.ofNullable(appVersion.getDescription()).orElseThrow(() -> new CustomerException("版本描述未添加"));
//        Optional.ofNullable(appVersion.getUrlVersion()).orElseThrow(() -> new CustomerException("版本下载链接未添加"));
//        Optional.ofNullable(appVersion.getIsForce()).orElseThrow(() -> new CustomerException("请确定是否强制更新"));¬¬
        appVersion.setCreateDate(new Date());
        int row = appVersionService.insertVersion(appVersion);
        if (row > 0) {
            return ServiceResponse.createSuccessResponse();
        }
        return ServiceResponse.createErrorResponse();
    }
}
